<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function People (name) {
            this.name = name
            return this
        }
        People.prototype.hello = function () {
            console.log('hello')
        }

        // var p = new People('xiaowang')
        // console.log(p)
        // p.hello()


        // 重写为 People.new('xiaowang') 这种创建方式

        Function.prototype.new = function () {
            var obj = Object.create(this.prototype) // 以指定原型的方式创建obj
            var result = this.apply(obj, arguments)
            return result
        }

        var p = People.new('xiaowang')
        console.log(p)
        p.hello()
    </script>
</body>

</html>